import {Component, ViewChild, NgZone} from '@angular/core';
import {Content, IonicPage, ViewController, InfiniteScroll, NavController} from 'ionic-angular';
import {PagingConfig, PagingProvider} from "@providers/paging";
import {NativeProvider} from "@providers/natives/native";
import {DialogProvider} from "@providers/dialog";
import {Util} from "@providers/util";
import {StorageProvider} from "@providers/storage";

@IonicPage()
@Component({
  templateUrl: 'sazyt-mmaterial.html',
})
export class SazytMmaterialPage {
  @ViewChild(Content) content: Content;
  isClick: boolean = false;//Popover是否显示
  //保存数据
  datas: any[] = [];
  datasConfig: PagingConfig = new PagingConfig();
  isScrollDown = false;
  //判断页面是否是向下滑动
  IS_APP: any = this.navtive.isCordova;
  query_field: any;
  datasCache: any[] = [];
  SFZX: any[] = [];

  constructor(private pagingService: PagingProvider,
              public zone: NgZone,
              private viewCtrl: ViewController,
              private navtive: NativeProvider,
              private dialog: DialogProvider,
              private navctrl: NavController,
              private storage: StorageProvider) {
    this.SFZX = this.storage.read("userInfo")['SFZX'];
  }


  ionViewDidLoad() {
    // this.initCacheOrder();
    this.datas = [];
    this.datasConfig = new PagingConfig();
    this.getData(data => {
      this.datas = data;
      this.datasCache = data;
    }, this.datasConfig);
  }

  dismiss(data?: any) {
    if (data) {
      this.viewCtrl.dismiss(data);
      return;
    }
    this.viewCtrl.dismiss();
  }

  /**
   * 滑动时触发
   */
  scrollContent() {
    this.zone.run(() => {
      this.isScrollDown = this.content.directionY == "down";
    });
  }


  /**
   * 刷新数据
   */
  refresh(refresher?: any) {
    this.datasConfig = new PagingConfig();
    this.getData(data => {
      this.datas = data;
      if (refresher) {
        setTimeout(() => {
          refresher.complete();
        }, 500);
      }
    }, this.datasConfig, error => {
      if (refresher) {
        setTimeout(() => {
          refresher.complete();
        }, 500);
      }
    });
  }

  getMoreDatas(infiniteScroll: InfiniteScroll) {
    this.getData((value) => {
      this.datas.push.apply(this.datas, value);
      infiniteScroll.complete();
    }, this.datasConfig);
  }

  private getData(fn: (value) => void, pagingConfig, errorFun?: (error) => void) {
    this.pagingService.get({
      interfaceId: "csm.css.jcdy.qcfwquery.selectPJJGB",
      data: {}
    }, pagingConfig).subscribe(data => {

      console.log(data);
      fn(data);
    }, error => {
      if (errorFun) {
        errorFun(error);
      }
    });
  }

  push(page) {
    this.navctrl.push(page , {datas: this.datas})
      .catch( () => {
     this.dialog.showAlert("页面加载失败");
    });
  }

  /**
   * 查询
   */
  query() {
    this.datas = this.datasCache;

    if(Util.isNull(this.query_field)) {
      return;
    }

    this.datas = this.datas.filter( v => {
      let field = this.query_field + "";
      let name = v.PRODUCT_NAME + "";
      let id = v.PRODUCT_ID + "";
      return name.includes(field) || id.includes(field , 0);
    });

  }

}






